<?php

function get_template($typeTpl, $params = null) {
    $html = null;
    switch ($typeTpl) {
        case 0:
            $html = get_list_image();
            break;
        default:
            break;
    }
    return $html;
}

function get_list_image() {
    global $tpl, $az_db;
    //Create list user
    $pager = new Az_Pager();
    $file_tpl = 'image/list_image';
    $listimg = $tpl->get_main($file_tpl, 2);

    //Create a $sql query for get all total category 
    $sql = "SELECT COUNT(*) FROM `az_file` WHERE `type_file`=1";
    $result = $az_db->executeSQL_Query($sql)->fetch_row();
    $totalRecord = $result[0];


    $rootDir = '../../../upload/image/';
    //Create params for pager
    $scroll_page = 5;
    $per_page = 18;
    $current_page = isset($_GET['page']) ? $_GET['page'] : 1;
    $url = 'index.php?page=';

    //Setup a page
    $pager->page_set($url, $totalRecord, $scroll_page, $per_page, $current_page);

    //Create $sql query for get record of a page
    $sql_P = "SELECT * FROM `az_file` WHERE `type_file`=1 ORDER BY `file_id` DESC LIMIT " . $pager->start . "," . $pager->per_page;
    $result_P = $az_db->executeSQL_Query($sql_P);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $imgId = $rows['file_id'];
            $imgname = $rows['name'];
            $imgInfo = $rows['info'];
            $name_file_type = $rows['name_file_type'];
            if ($name_file_type === 'jpeg') {
                $dirimg = $rootDir . 'jpg/root/';
            } else if ($name_file_type === 'gif') {
                $dirimg = $rootDir . 'gif/root/';
            } else if ($name_file_type === 'png') {
                $dirimg = $rootDir . 'png/root/';
            }
            $img_data .= '<div class="frm_img_div_img_item"><ul class="frm_list_img_ul">' .
                    '<li><a href="' . $dirimg . $imgname . '" rel="lightbox"><img src="' . $dirimg . $imgname . '" alt="Thư pháp" width="70" height="70" /></a></li>' .
                    '<li class="frm_img_item_info"><input type="checkbox" name="image[]" value="'.$imgId.'"/>' . $imgname . '&nbsp;' . $imgInfo . '</li></ul></div>';
        }
        $result_P->free();
        //Assign $html
        $html = $tpl->get_vars($listimg, array('list_img_block' => $img_data, 'page_par_left' => $pager->firt_page,
                    'page_left' => $pager->previous_page, 'page-info' => $pager->page_info,
                    'page-right' => $pager->next_page, 'page-far-right' => $pager->last_page));
        return $html;
    } else {
        $result_P->free();
        return $listimg;
    }
}

?>
